﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class AudioInspector : RecordEventBaseInspector
    {
        private Label audioClipLenth;

        private AudioEvent audioData;

        public override void Show(VisualElement root)
        {
            this.root = root;
            itemStyle = ActionEditorInspector.currentTrackItemStyle;
            data = itemStyle.eventData;
            audioData = data as AudioEvent;

            ShowRecord();
            audioClipLenth = new Label("音效原始播放帧数");
            base.root.Add(audioClipLenth);
            ShowTrackName();
            ShowAudioClip();
            ShowStartIndex();
            ShowDurationIndex();
            ShowPosition();

            ShowAnimationInfo();

        }

        private void ShowAudioClip()
        {
            ObjectField animationClipAssetField = new ObjectField("音效资源");
            animationClipAssetField.objectType = typeof(AudioClip);
            animationClipAssetField.value = audioData.audioClip;
            animationClipAssetField.RegisterValueChangedCallback(p =>
            {
                if (p.newValue == null) return;
                AudioClip clip = p.newValue as AudioClip;
                // 修改自身显示效果
                audioClipLenth.text = "音效原始播放帧数:      " +
                                      ((int)(audioData.audioClip.length * ActionEditor.Instance.ActionData.FrameRote));
                // 保存到配置
                audioData.audioClip = clip;
                audioData.audioPath = AssetDatabase.GetAssetPath(clip);
                audioData.durationIndex = (int)(clip.length * ActionEditor.Instance.ActionData.FrameRote);
                itemStyle.ResetView();
            });
            root.Add(animationClipAssetField);
        }

        private void ShowAnimationInfo()
        {
            if (audioData.audioClip != null)
            {
                AudioClip clip = audioData.audioClip;
                audioClipLenth.text = "音效原始播放帧数:      " +
                                      ((int)(audioData.audioClip.length * ActionEditor.Instance.ActionData.FrameRote));
            }
        }

        private void ShowPosition()
        {
            Vector3Field positon = new Vector3Field("位置");
            positon.value = audioData.position;
            positon.RegisterCallback<FocusOutEvent>(p =>
            {
                if (audioData.position != positon.value)
                {
                    audioData.position = positon.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(positon);
        }
    }
}
